/**

 * 版权所有(C)，上海勾芒信息科技，2017，所有权利保留。
 *
 * 项目名：cards-web 
 * 文件名：:MemberReigisterEventListener.java
 * 模块说明：	
 * 修改历史：
 * 2017年9月18日 - liyang - 创建。
 *
 */
package com.thor.palmmall.rest.event.listener;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.eventbus.Subscribe;
import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.palmmall.card.common.CardServiceConfig;
import com.thor.palmmall.card.common.SOperateInfoConverter;
import com.thor.palmmall.card.member.CheckAccountRequest;
import com.thor.palmmall.card.member.CheckAccountRequest.SAccountType;
import com.thor.palmmall.card.member.OpenCardRequest;
import com.thor.palmmall.card.member.OpenCardRequest.CardInfo;
import com.thor.palmmall.card.member.OpenCardRequest.ExtMember;
import com.thor.palmmall.card.member.SMember;
import com.thor.palmmall.card.member.SMember.SAddress;
import com.thor.palmmall.card.member.SMember.SBirthday;
import com.thor.palmmall.card.member.SMember.SRegisterAddress;
import com.thor.palmmall.card.member.SMemberService;
import com.thor.palmmall.rest.event.MemberRegisterEvent;
import com.thor.palmmall.rest.order.AOrder;
import com.thor.palmmall.util.event.Event;
import com.thor.palmmall.util.event.EventHandler;
import com.thor.palmmall.util.event.GlobalEventBus;

/**
 * @author:liyang
 * @time:2017年9月18日 上午11:58:52
 * @since:1.0
 */
public class MemberRegisiterEventListener implements EventHandler {

  private Logger logger = Logger.getLogger(MemberRegisiterEventListener.class);

  @PostConstruct
  public void init() {
    GlobalEventBus.register(this);
  }

  @Override
  @Subscribe
  public void process(Event event) throws Exception {

    if (!(event instanceof MemberRegisterEvent)) {
      return;
    }
    AOrder order = ((MemberRegisterEvent) event).getOrder();
    BeanOperateInfo operateInfo = ((MemberRegisterEvent) event).getOperateInfo();
    if (order == null || operateInfo == null) {
      return;
    }

    try {
      CheckAccountRequest req = new CheckAccountRequest();
      SAccountType accountType = new SAccountType();
      accountType.setId(order.getMemberCardNo());
      req.setAccout(accountType);
      req.setOperCtx(SOperateInfoConverter.getInstance().convert(operateInfo));
      boolean exists = sMemberService.checkAccountExist(req);
      if (!exists) {
        // 创建新会员
        createNewMember(order, operateInfo);
      } else {
        logger.info("会员已经存在,会员手机号为:" + order.getMemberCardNo());
      }
    } catch (Exception e) {
      logger.error("-----------------创建新会员失败,错误原因:----------------" + e.getMessage());
    }

  }

  private void createNewMember(final AOrder order, BeanOperateInfo operateInfo)
      throws ThorServiceException {

    ExtMember extMbr = new ExtMember();
    extMbr.setCardId(cardServiceConfig.getCardId());
    extMbr.setOpenId(order.getMemberCardNo());
    SMember member = new SMember();
    member.setCellphone(order.getMemberCardNo());
    member.setTelephone(order.getMemberCardNo());
    member.setName(order.getCustomerName());
    member.setAddress(order.getReceiptAddress());
    member.setBirthday(new SBirthday(2000, 1, 1));
    member.setGender("unknown");
    member.setWedLock("secret");
    member.setBelongStore(cardServiceConfig.getBelongStore());
    List<SAddress> deliverAddressList = new ArrayList<SAddress>();
    deliverAddressList.add(new SAddress());
    member.setDeliveryAddress(deliverAddressList);
    member.setRegisterAddress(new SRegisterAddress());

    CardInfo cardInfo = new CardInfo();
    cardInfo.setExtMember(extMbr);
    cardInfo.setMember(member);

    OpenCardRequest openCardReq = new OpenCardRequest();
    openCardReq.setOperCtx(SOperateInfoConverter.getInstance().convert(operateInfo));
    openCardReq.setRequest(cardInfo);
    sMemberService.openCard(openCardReq);
  }

  @Autowired
  private SMemberService sMemberService;

  @Autowired
  CardServiceConfig cardServiceConfig;

}
